<?php
if (! defined ( 'IS_INITPHP' ))
	exit ( 'Access Denied!' );
require_once (INITPHP_PATH . '/library/getui/IGt.Push.php');
// 导入个推核心类文件
class getuiInit {
	private $host = 'http://sdk.open.api.igexin.com/apiex.htm';
	private $softid;
	private $getui_appkey;
	private $getui_appid;
	private $getui_appsecret;
	private $getui_mastersecret;
	private $ios_softid = array ( // IOS的softid,1-美妆相机 iPhone版 ;3-潮自拍 iPhone版;
			1,
			3,
			5
	);
	function config($type, $istest) {
		$this->softid = $type;
		$config = InitPHP::getConfig ();
		$getuiConf = $config ['getui'];
		if ($istest) {
			$this->getui_appkey = $getuiConf [$type] ['test'] ['appkey'];
			$this->getui_appid = $getuiConf [$type] ['test'] ['appid'];
			$this->getui_appsecret = $getuiConf [$type] ['test'] ['appsecret'];
			$this->getui_mastersecret = $getuiConf [$type] ['test'] ['mastersecret'];
		} else {
			$this->getui_appkey = $getuiConf [$type] ['appkey'];
			$this->getui_appid = $getuiConf [$type] ['appid'];
			$this->getui_appsecret = $getuiConf [$type] ['appsecret'];
			$this->getui_mastersecret = $getuiConf [$type] ['mastersecret'];
		}
	}
	function getUserStatus($token) {
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		$rep = $igt->getClientIdStatus ( $this->getui_appid, $token );
		var_dump ( $rep );
		echo ("<br><br>");
	}
	
	/**
	 * 点击通知打开应用
	 *
	 * @return IGtNotificationTemplate
	 */
	function IGtNotificationTemplateDemo($msg) {
		$template = new IGtNotificationTemplate ();
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionType ( 1 ); // 透传消息类型
		$template->set_transmissionContent ( "测试离线" ); // 透传内容
		$template->set_title ( "您有一条新的消息" ); // 通知栏标题
		$template->set_text ( $msg ); // 通知栏内容
		$template->set_logo ( "http://wwww.igetui.com/logo.png" ); // 通知栏logo
		$template->set_isRing ( true ); // 是否响铃
		$template->set_isVibrate ( true ); // 是否震动
		$template->set_isClearable ( true ); // 通知栏是否可清除
		return $template;
	}
	
	/**
	 * 发送简单文本通知
	 *
	 * @param unknown $cid        	
	 * @param unknown $payload        	
	 * @return boolean
	 */
	function pushTextMessageToOne($cid, $payload) {
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		
		// 消息类型 : 透传信息
		$template = $this->IGtNotificationTemplateDemo ( $payload );
		//
		// 个推信息体
		$message = new IGtSingleMessage ();
		
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		                                  // 接收方
		$target = new IGtTarget ();
		$target->set_appId ( $this->getui_appid );
		$target->set_clientId ( $cid );
		
		$rep = $igt->pushMessageToSingle ( $message, $target );
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload . '||cid:' . $cid, 'getui_error' );
			return false;
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload . '||cid:' . $cid, 'getui' );
			return true;
		}
	}
	
	/**
	 * 个推群推接口
	 *
	 * @param array $cid
	 *        	个推的token列表，最多5000个
	 * @param string $payload
	 *        	透传内容
	 */
	public function pushMessageToList($cid, $payload) {
		// putenv("needDetails=true");
		$t = urldecode ( $payload ['pt'] );
		$payload2 = urldecode ( json_encode ( $payload ) );
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		$template = new IGtTransmissionTemplate (); // 消息类型 : 透传信息
		$template->set_transmissionType ( 2 ); // 透传消息类型
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionContent ( $payload2 ); // 透传内容
		if (in_array ( $this->softid, $this->ios_softid )) {
			$c = urldecode ( $payload ['pc'] ? $payload ['pc'] : '' );
			unset ( $payload ['pt'] );
			unset ( $payload ['pc'] );
			$p = urldecode ( json_encode ( $payload ) );
			$sound = $this->softid == 3 ? "bs.mp3" : "default";
			$template->set_pushInfo ( null, 1, $c, $sound, $p, null, null, 0 );
		}
		$message = new IGtListMessage ();
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		                                  // 接收方
		$contentId = $igt->getContentId ( $message );
		$targetList = array ();
		foreach ( $cid as $v ) {
			$target = new IGtTarget ();
			$target->set_appId ( $this->getui_appid );
			$target->set_clientId ( $v );
			$targetList [] = $target;
		}
		$rep = $igt->pushMessageToList ( $contentId, $targetList );
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload2 . '||cid:' . json_encode ( $cid ) . "\r\n", 'outpushlist_error' );
			return false;
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload2 . '||cid:' . json_encode ( $cid ) . "\r\n", 'outpushlist' );
			return true;
		}
	}
	
	/**
	 * 发送透传消息
	 *
	 * @param unknown $cid        	
	 * @param unknown $payload        	
	 * @return boolean
	 */
	function pushMessageToSingle($cid, $payload) {
		$t = urldecode ( $payload ['pt'] );
		$payload2 = urldecode ( json_encode ( $payload ) );
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		
		// 消息类型 : 透传信息
		$template = new IGtTransmissionTemplate ();
		
		$template->set_transmissionType ( 2 ); // 透传消息类型
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionContent ( $payload2 ); // 透传内容
		                                                  //
		                                                  // 个推信息体
		if (in_array ( $this->softid, $this->ios_softid )) {
			$c = urldecode ( $payload ['pc'] ? $payload ['pc'] : '' );
			unset ( $payload ['pt'] );
			unset ( $payload ['pc'] );
			$p = urldecode ( json_encode ( $payload ) );
			$sound = $this->softid == 3 ? "bs.mp3" : "default";
			$template->set_pushInfo ( null, 1, $c, $sound, $p, null, null, 0 );
		}
		$message = new IGtSingleMessage ();
		
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		                                  // 接收方
		$target = new IGtTarget ();
		$target->set_appId ( $this->getui_appid );
		$target->set_clientId ( $cid );
		
		$rep = $igt->pushMessageToSingle ( $message, $target );
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload2 . '||cid:' . $cid . "\r\n", 'out_push_error' );
			return false;
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload2 . '||cid:' . $cid . "\r\n", 'out_push' );
			return true;
		}
	}
	/**
	 * 发送透传消息
	 *
	 * @param unknown $payload        	
	 * @return boolean
	 */
	function pushMessageToAll($payload,$tag=false) {
		$t = urldecode ( $payload ['pt'] );
		$payload2 = urldecode ( json_encode ( $payload ) );
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		// 消息类型 : 透传信息
		$template = new IGtTransmissionTemplate ();
		$template->set_transmissionType ( 2 ); // 透传消息类型
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionContent ( $payload2 ); // 透传内容
		if (in_array ( $this->softid, $this->ios_softid )) {
			$c = urldecode ( $payload ['pc'] ? $payload ['pc'] : '' );
			unset ( $payload ['pt'] );
			unset ( $payload ['pc'] );
			$p = urldecode ( json_encode ( $payload ) );
			$sound = "default";
			$template->set_pushInfo ( null, 1, $c, $sound, $p, null, null, 0 );
		}
		$message = new IGtAppMessage();
		
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		$message->set_appIdList(array($this->getui_appid));
		if($tag){
			$message->set_tagList(array($tag));
		}
		                                  // 接收方
		$rep=$igt->pushMessageToApp($message);
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload2 . "\r\n", 'out_push_error' );
			return false;
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload2 . "\r\n", 'out_push' );
			return true;
		}
	}
	
	/**
	 * 发送透传消息
	 *
	 * @param unknown $cid        	
	 * @param unknown $payload        	
	 * @return boolean
	 */
	function pushMessageToSingle_ad($cid, $payload) {
		$payload = json_encode ( $payload );
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		
		// 消息类型 : 透传信息
		$template = new IGtTransmissionTemplate ();
		
		$template->set_transmissionType ( 2 ); // 透传消息类型
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionContent ( $payload ); // 透传内容
		                                                 //
		                                                 // 个推信息体
		$message = new IGtSingleMessage ();
		
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		                                  // 接收方
		$target = new IGtTarget ();
		$target->set_appId ( $this->getui_appid );
		$target->set_clientId ( $cid );
		
		$rep = $igt->pushMessageToSingle ( $message, $target );
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload . '||cid:' . $cid . "\r\n", 'getui_error' );
			return false;
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload . '||cid:' . $cid . "\r\n", 'getui' );
			return true;
		}
	}
	
	/**
	 * 发送透传消息
	 *
	 * @param unknown $cid        	
	 * @param unknown $payload        	
	 * @return boolean
	 */
	function pushMessageToSingle_ios($cid, $payload, $lang = "zh_cn") {
		if ($lang == 'zh_cn') {
			$lang = 'zh';
		}
		if ($lang == 'zh_tw') {
			$lang = 'tw';
		}
		$title [1] = array (
				'zh' => '美老师回复了你的问题，快来看看吧。',
				'tw' => '美老師回覆你的問題，快來看看吧。',
				'en' => 'Miss beauty has replied to your question, take a look at the response now. ' 
		);
		$title [3] = array (
				'zh' => '您的意见反馈收到了回复啦，赶快看看吧～',
				'tw' => '您的意見反應收到回覆啦，趕快看看吧~',
				'en' => 'We have responded to your feedback, come and take a look!',
				'kor' => '당신의 피드백에 대한 답장이 도착했습니다, 빨리 확인하십시오~',
				'jp' => 'あなたのフィードバックにご返答させて頂きました！ 是非ご確認ください。' 
		);
		
		$t = $title [$this->softid] [$lang];
		$payload = json_encode ( $payload );
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		// 消息类型 : 透传信息
		$template = new IGtTransmissionTemplate ();
		
		$template->set_transmissionType ( 2 ); // 透传消息类型
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionContent ( $payload ); // 透传内容
		
		$template->set_pushInfo ( null, 1, $t, "default", $payload, null, null, 0 );
		
		//
		// 个推信息体
		$message = new IGtSingleMessage ();
		
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		                                  
		// 接收方
		$target = new IGtTarget ();
		$target->set_appId ( $this->getui_appid );
		$target->set_clientId ( $cid );
		
		$rep = $igt->pushMessageToSingle ( $message, $target );
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload . '||cid:' . $cid, 'getui_error' );
			return false;
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload . '||cid:' . $cid, 'getui' );
			return true;
		}
	}
	
	/**
	 * 指定应用推送消息
	 * wangchj
	 * 2014-10-21
	 * *
	 */
	public function pushMessageToApp($payload) {
		$payload = urldecode ( json_encode ( $payload ) );
		$igt = new IGeTui ( $this->host, $this->getui_appkey, $this->getui_mastersecret );
		// 消息模版：
		// 1.TransmissionTemplate:透传功能模板
		// 2.LinkTemplate:通知打开链接功能模板
		// 3.NotificationTemplate：通知透传功能模板
		
		// $template = IGtLinkTemplateDemo();
		// $template = new IGtNotificationTemplate();
		$template = new IGtTransmissionTemplate ();
		
		$template->set_transmissionType ( 2 ); // 透传消息类型
		$template->set_appId ( $this->getui_appid ); // 应用appid
		$template->set_appkey ( $this->getui_appkey ); // 应用appkey
		$template->set_transmissionContent ( $payload ); // 透传内容
		                                                 // $template->set_title("river"); //通知栏标题
		                                                 // $template->set_text("wlxj"); //通知栏内容
		                                                 // $template->set_logo($url); //通知栏logo
		                                                 // $template->set_isRing(true); //是否响铃
		                                                 // $template->set_isVibrate(true); //是否震动
		                                                 // $template->set_isClearable(true); //通知栏是否可清除
		                                                 //
		                                                 // 个推信息体
		$message = new IGtAppMessage ();
		
		$message->set_isOffline ( true ); // 是否离线
		$message->set_offlineExpireTime ( 3600 * 12 * 1000 ); // 离线时间;12小时
		$message->set_data ( $template ); // 设置推送消息类型
		                                  // 接收方
		$message->set_appIdList ( array (
				$this->getui_appid 
		) );
		// $message->set_phoneTypeList(array('ANDROID'));
		// $message->set_provinceList(array('浙江','北京','河南'));
		// $message->set_tagList(array('开心'));
		
		$rep = $igt->pushMessageToApp ( $message );
		if ($rep ['result'] != 'ok') {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload, 'getui_error' );
		} else {
			writeLog ( json_encode ( $rep ) . '||payload:' . $payload, 'getui' );
		}
		return $rep;
		// return true
	}
}